/**
 * 微信小程序下的ECharts适配器
 */

// 全局缓存canvas对象
const canvasMap = new Map();

// 根据canvasId获取画布
export function getCanvas(canvasId, ctx, width, height, dpr) {
  const canvas = {
    getContext: () => ctx,
    toDataURL: () => '',
    addEventListener: () => {},
    removeEventListener: () => {},
    dispatchEvent: () => {},
    requestAnimationFrame: callback => setTimeout(callback, 16),
    cancelAnimationFrame: handle => clearTimeout(handle),
    width,
    height,
    style: {
      width: `${width}px`,
      height: `${height}px`
    }
  };
  
  // 保存到缓存
  canvasMap.set(canvasId, canvas);
  
  return canvas;
}

// 处理触摸事件
export function wrapTouch(event) {
  const touches = event.touches.map(touch => {
    return {
      identifier: touch.identifier,
      pageX: touch.pageX,
      pageY: touch.pageY,
      clientX: touch.clientX,
      clientY: touch.clientY,
      x: touch.x || touch.pageX,
      y: touch.y || touch.pageY
    };
  });
  
  const changedTouches = event.changedTouches.map(touch => {
    return {
      identifier: touch.identifier,
      pageX: touch.pageX,
      pageY: touch.pageY,
      clientX: touch.clientX,
      clientY: touch.clientY,
      x: touch.x || touch.pageX,
      y: touch.y || touch.pageY
    };
  });
  
  return {
    touches,
    changedTouches,
    type: event.type,
    timeStamp: event.timeStamp
  };
}

// 释放Canvas缓存
export function releaseCanvas(canvasId) {
  canvasMap.delete(canvasId);
} 